package gnt.sd.view;

import gnt.sd.model.YoutubeData;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SDYoutubeAdapter extends ArrayAdapter<YoutubeData> implements
		OnScrollListener {
	ArrayList<YoutubeData> _listSong;
	SDYoutubeTableCell[] _listCache;
	int _resource;
	Context _context;
	ListView _listView;
	boolean _isScroll;
	public SDYoutubeAdapter(ListView listView, Context context,
			int textViewResourceId, ArrayList<YoutubeData> objects) {
		super(context, textViewResourceId, objects);
		this._listSong = objects;
		this._context = context;
		this._resource = textViewResourceId;
		_listCache = new SDYoutubeTableCell[_listSong.size()];
//		_listView = listView;
//		_listView.setOnScrollListener(this);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return _listSong.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (_listCache[position] == null) {
			convertView = new SDYoutubeTableCell(_context,
					_listSong.get(position));
			final YoutubeData youtubeData = _listSong.get(position);
			if (youtubeData != null) {
				TextView line1 = ((SDYoutubeTableCell) convertView).line1;
				TextView line2 = ((SDYoutubeTableCell) convertView).line2;
				line1.setText(youtubeData.getVideoName());
				SDYoutubeView youtubeView = ((SDYoutubeTableCell) convertView).youtubeView;
				if (youtubeData.getThumbnailLink() != null && !_isScroll)
					youtubeView.getImage(youtubeData);
				int dur = Integer.parseInt(youtubeData.getDuration());
				String min = String.valueOf(dur / 60);
				String sec = String.valueOf(dur % 60);
				if (youtubeData.getViewCount() != null) {
					line2.setText(min + ":" + sec + " - "
							+ youtubeData.getViewCount() + " views");
				} else {
					line2.setText(min + ":" + sec);
				}
			}
			_listCache[position] = (SDYoutubeTableCell) convertView;
		}
		return _listCache[position];
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		switch (scrollState) {
		case OnScrollListener.SCROLL_STATE_IDLE:
			int first = view.getFirstVisiblePosition();
//			while(view.getAdapter().getItemViewType(first) != view.getAdapter().IGNORE_ITEM_VIEW_TYPE) {
//				first ++;
//			}
			int last = view.getLastVisiblePosition();
//			while(view.getChildAt(last).getClass() != YoutubeTableCell.class) {
//				last --;
//			}
			view.getAdapter().getItemViewType(first);
			int count = last - first ;
			for (int i = 0; i < count; i++) {
				final YoutubeData youtubeData = _listSong.get(first + i);
				if (youtubeData != null) {
					SDYoutubeTableCell convertView = (SDYoutubeTableCell) view
							.getChildAt(i);
					TextView line1 = ((SDYoutubeTableCell) convertView).line1;
					TextView line2 = ((SDYoutubeTableCell) convertView).line2;
					SDYoutubeView youtubeView = ((SDYoutubeTableCell) convertView).youtubeView;
					line1.setText(youtubeData.getVideoName());
					if (youtubeData.getThumbnailLink() != null)
						youtubeView.getImage(youtubeData);
					int dur = Integer.parseInt(youtubeData.getDuration());
					String min = String.valueOf(dur / 60);
					String sec = String.valueOf(dur % 60);
					if (youtubeData.getViewCount() != null) {
						line2.setText(min + ":" + sec + " - "
								+ youtubeData.getViewCount() + " views");
					} else {
						line2.setText(min + ":" + sec);
					}
				}
			}
			break;
		case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
			_isScroll = true;
			break;
		case OnScrollListener.SCROLL_STATE_FLING:
			_isScroll = true;
			break;
		}

	}

}
